package com.swt.testdemo.exception;

import cn.hutool.core.text.StrBuilder;
import com.swt.testdemo.config.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.redis.connection.PoolException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 全局异常处理
 *
 * @author BleethNie
 * @version 1.0
 * @date 2020-01-10 17:42
 **/
@Slf4j
@RestControllerAdvice
public class AgyExceptionHandler {

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(AgyException.class)
    public ResponseEntity<ResponseMessage> handleException(AgyException e) {
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        ResponseMessage message = new ResponseMessage(status.value(), e.getMessage());
        return ResponseEntity.status(message.getStatus()).body(message);
    }



    @ExceptionHandler(NoHandlerFoundException.class)
    public ResponseEntity<?> handlerNoFoundException(Exception e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("路径不存在，请检查路径是否正确");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseEntity<ResponseMessage> handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        String log = "数据库中已存在该记录";
        ResponseMessage message = new ResponseMessage(status.value(), log);
        return ResponseEntity.status(status).body(message);
    }

    @ExceptionHandler({AuthorizationException.class,UnauthorizedException.class})
    public ResponseEntity<?> handleAuthorizationException(AuthorizationException e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body("当前用户没有访问权限");
    }



    @ExceptionHandler(Exception.class)
    public ResponseEntity<?> handleException(Exception e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败，" + e.getMessage());
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseEntity<?> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        StrBuilder strBuilder = new StrBuilder();
        strBuilder.append("不支持");
        strBuilder.append(e.getMethod());
        strBuilder.append("请求方法，");
        strBuilder.append("支持以下");
        String[] methods = e.getSupportedMethods();
        if (methods != null) {
            for (String str : methods) {
                strBuilder.append(str);
                strBuilder.append("、");
            }
        }
        log.error(strBuilder.toString(), e);
        return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(strBuilder.toString());
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResponseEntity<?> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件大小超出限制, 请压缩或降低文件质量! ");
    }

    @ExceptionHandler(DataIntegrityViolationException.class)
    public ResponseEntity<?> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("字段太长,超出数据库字段的长度");
    }

    @ExceptionHandler(PoolException.class)
    public ResponseEntity<?> handlePoolException(PoolException e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Redis 连接异常!");
    }

}
